package com.example.moneygrosService;

import com.example.moneygrosService.requests.MoneyGrosRequest;
import com.example.moneygrosService.requests.MoneyGrosRequestProcessing;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class SmsReceiver extends BroadcastReceiver {
	private static final String TAG = "SmsReceiver";

	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub

		Bundle bundle=intent.getExtras();

		Object[] messages=(Object[])bundle.get("pdus");
		SmsMessage[] sms=new SmsMessage[messages.length];

		for(int n=0;n<messages.length;n++){
			sms[n]=SmsMessage.createFromPdu((byte[]) messages[n]);
		}

		for(SmsMessage msg:sms){

			//			ReceiveSMSActivity.updateMessageBox("\nFrom: "+msg.getOriginatingAddress()+"\n"+
			//					"Message: "+msg.getMessageBody()+"\n");
			if(msg.getMessageBody().startsWith("MONEYGROS")){
				this.abortBroadcast();
				Log.v(TAG, "SMS MONEYGROS recu de "+ msg.getOriginatingAddress());
				Toast.makeText(context, "SMS MONEYGROS recu de "+ msg.getOriginatingAddress(), Toast.LENGTH_LONG).show();

				MoneyGrosRequest request = new MoneyGrosRequest(msg.getMessageBody());
				MoneyGrosRequestProcessing.processRequest(request);
			}else {
				Log.v(TAG, "SMS recu de "+ msg.getOriginatingAddress());
				Toast.makeText(context, "SMS recu de "+ msg.getOriginatingAddress(), Toast.LENGTH_SHORT).show();
			}
			//			faire un appel au handler ou au service correspondant..

		}

	}


}
